import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import './assets/main.css'
import { h } from 'vue'
import api from './api'

// 配置axios默认请求头和拦截器
api.interceptors.request.use(config => {
  const token = localStorage.getItem('token');
  if (token) {
    config.headers.Authorization = `Bearer ${token}`;
  }
  return config;
});

// 全局配置Element Plus
const elementPlusOptions = {
  zIndex: 10000,  // 设置组件的基础z-index
  size: 'default'
}

// 检查用户登录状态
const checkAuth = async () => {
  const token = localStorage.getItem('token')
  if (token) {
    try {
      // 用token获取当前用户信息
      const response = await api.get('/auth/me')
      if (response.data && response.data.status === 'success') {
        // 存储用户信息到store
        store.commit('setUser', response.data.data)
      } else {
        // 如果获取失败，清除token
        localStorage.removeItem('token')
      }
    } catch (error) {
      console.error('获取用户信息失败', error)
      // 如果发生错误，清除token
      localStorage.removeItem('token')
    }
  }
}

// 模拟ElementPlus图标
const app = createApp(App)

app.component('ElIconMenu', {
  render() {
    return h('span', { class: 'el-icon-menu' })
  }
})

app.component('ElIconFolder', {
  render() {
    return h('span', { class: 'el-icon-folder' })
  }
})

app.component('ElIconDocument', {
  render() {
    return h('span', { class: 'el-icon-document' })
  }
})

app.component('ElIconVideoPlay', {
  render() {
    return h('span', { class: 'el-icon-video-play' })
  }
})

app.component('ElIconArrowDown', {
  render() {
    return h('span', { class: 'el-icon-arrow-down' })
  }
})

app.component('ElIconTime', {
  render() {
    return h('span', { class: 'el-icon-time' })
  }
})

app.component('ElIconCheck', {
  render() {
    return h('span', { class: 'el-icon-check' })
  }
})

app
  .use(router)
  .use(store)
  .use(ElementPlus, elementPlusOptions)
  .mount('#app')

// 应用挂载后检查登录状态
checkAuth() 